"""
Case Type   : reverse函数
Case Name   : reverse()参数传入中文字符
Create At   : 2022/7/14
Owner       : @songjing20
Description :
    1.reverse()传入中文字符
    2.清理环境
Expect      :
    1.返回逆序的字符串
    2.成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Sysfunction(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.commsh = CommonSH('dbuser')
        self.constant = Constant()
        self.db_name = 'db_ustore_reverse_0019'
        create_db_res = self.commsh.execut_db_sql(
            f"create database {self.db_name} encoding 'utf8';")
        self.log.info(create_db_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_db_res,
                      f'预置utf8字符编码数据库失败')

    def test_regexp_substr(self):
        text = '-----step1:reverse()参数传入中文字符;expect:返回逆序的字符串-----'
        self.log.info(text)
        res = self.commsh.execut_db_sql(
            f"set enable_default_ustore_table = on;"
            f"select case when reverse('中国')='国中' then 'a' else 'b' end;"
            f"select reverse('。@#￥……&*《尚书》！');"
            f"set enable_default_ustore_table = off;", dbname=self.db_name)
        self.log.info(res)
        self.assertIn("a\n(1 row)", res, f"执行失败:{text}")
        self.assertIn("！》书尚《*&……￥#@。\n(1 row)", res, f"执行失败:{text}")

    def tearDown(self):
        text = '-----step2:清理环境;expect:成功-----'
        self.log.info(text)
        drop_res = self.commsh.execut_db_sql(f"drop database {self.db_name};")
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res,
                      f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
